package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdut.material.pojo.Production;
import com.sdut.material.mapper.ProductionMapper;
import com.sdut.material.pojo.vo.LayUISelectMVO;
import com.sdut.material.service.IProductionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class ProductionServiceImpl extends ServiceImpl<ProductionMapper, Production> implements IProductionService {
    @Autowired
    private ProductionMapper productionMapper;
    @Override
    public JSONResult selectAllProductions() {
        List<Production> productions = productionMapper.selectList(new QueryWrapper<>());
        List<LayUISelectMVO> list = new ArrayList<>();
        for (Production production : productions) {
            LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
            layUISelectMVO.setId(production.getId());
            layUISelectMVO.setName(production.getName());
            layUISelectMVO.setStatus(1);
            list.add(layUISelectMVO);
        }
        return JSONResult.ok(list);
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        IPage<Production> iPage = new Page<>(page,limit);
        IPage<Production> result = productionMapper.selectPage(iPage,new QueryWrapper<>());
        return LayUITableResult.ok(result.getTotal(),result.getRecords());
    }

    @Override
    public JSONResult insert(Production production) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",production.getName());
        if (productionMapper.selectOne(queryWrapper) != null) {
            return JSONResult.error("该名称已存在添加失败");
        }
        productionMapper.insert(production);
        return JSONResult.ok("添加成功");
    }

    @Override
    public Production selectById(Integer productionId) {
        Production production = productionMapper.selectById(productionId);
        return production;
    }

    @Override
    public void updateProduction(Production production) {
        productionMapper.updateById(production);
    }

    @Override
    public void deleteById(Integer productionId) {
        productionMapper.deleteById(productionId);
    }

}
